//
//  SZLoginTool.m
//  KaiJing
//
//  Created by lanou on 16/1/19.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "SZLoginTool.h"

@implementation SZLoginTool

+ (instancetype)shareLogin {
    static SZLoginTool *login = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        login = [[SZLoginTool alloc] init];
    });
    return login;
}

/**
 *  保存用户数据
 *
 *  @param model 数据模型
 */
- (void)saveUserMessageWithModel:(SZLoginMessageModel *)model {
    NSUserDefaults *defaut = [NSUserDefaults standardUserDefaults];
    [defaut setObject:model.uid forKey:@"uid"];
    [defaut setObject:model.phone forKey:@"phone"];
    [defaut setObject:model.countlikedata forKey:@"countlikedata"];
    [defaut setObject:model.countvisitdata forKey:@"countvisitdata"];
    [defaut setObject:model.username forKey:@"username"];
    [defaut synchronize];
}

/**
 *  保存登陆状态
 *
 *  @param loginState 传入非"1"既登陆失败
 */
- (void)saveLoginStateWithString:(NSString *)loginState {
    NSUserDefaults *defaut = [NSUserDefaults standardUserDefaults];
    if ([loginState isEqualToString:@"1"]) {
        [defaut setObject:@"YES" forKey:@"loginState"];
    } else {
        [defaut setObject:@"NO" forKey:@"loginState"];
    }
    [defaut synchronize];
}


/**
 *  判断是否登陆
 */
- (BOOL)isLogin {
    NSString *loginState = [[NSUserDefaults standardUserDefaults] valueForKey:@"loginState"];
    if ([loginState isEqualToString:@"YES"]) {
        return YES;
    } else {
        return NO;
    }
}


/**
 *  返回本地存储formhash
 */
- (NSString *)formhash {
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"formhash"];
}
/**
 *  存储formhash
 */
- (void)saveFormhash:(NSString *)formhash {
    [[NSUserDefaults standardUserDefaults] setObject:formhash forKey:@"formhash"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/**
 *  退出登陆更新formhash
 */
- (void)updateFormhash {
    [PKRequestManager requestWithUrlString:@"http://morguo.com/home.php?mod=space&do=profile&from=space&androidflag=1&appfrom=ios&iosversion=3.13" parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        [[SZLoginTool shareLogin] saveFormhash:dataDic[@"data"][@"formhash"]];
    } error:^(NSError *error) {
        SZLog(@"获取formhash失败");
    }];
}


- (NSString *)userName {
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"username"];
}
@end
